{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Lex chatbot creation from Amplify CLI",
    "Parameters": {
        "authRoleName": {
            "Type":  "String"
        },
        "unauthRoleName": {
            "Type":  "String"
        },
        "authRoleArn": {
            "Type":  "String"
        },
        "env": {
            "Type":  "String"
        }
    },
    "Mappings":  <%- JSON.stringify(props.mappings, null, 4) %>,
    "Metadata": {
        "AWS::CloudFormation::Interface": {
            "ParameterGroups": [
                {
                    "Label": {
                        "default": "Creating lex chatbot"
                    },
                    "Parameters": [
                        "inputs"
                    ]
                }
            ]
        }
    },
    "Conditions": {
        "ShouldNotCreateEnvResources": {
            "Fn::Equals": [
                {
                    "Ref": "env"
                },
                "NONE"
            ]
        }
    },
    "Resources": {
        "LambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Handler": "index.handler",
                    "FunctionName": {
                    "Fn::If": [
                        "ShouldNotCreateEnvResources",
                        "<%= props.functionName %>",
                        {

                            "Fn::Join": [
                                "",
                                [
                                    "<%= props.functionName %>",
                                    "-",
                                    {
                                        "Ref": "env"
                                    }
                                ]
                            ]
                        }
                    ]
                },
                "Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },
                "Environment": {"Variables" : { "ENV": {"Ref": "env"}}},
                "Runtime": "nodejs22.x",
                "Timeout": 300
            }
        },
        "LambdaCloudWatchPolicy": {
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": "<%= props.cloudWatchPolicyName %>",
                "Roles": [
                    {
                        "Ref": "LambdaExecutionRole"
                    }
                ],
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": [
                                "logs:CreateLogGroup",
                                "logs:CreateLogStream",
                                "logs:PutLogEvents"
                            ],
                            "Resource": {
                                "Fn::Sub": [
                                    "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${lambdaName}:log-stream:*",
                                    {
                                        "lambdaName": {
                                            "Ref": "LambdaFunction"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        },
        "LambdaExecutionRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "RoleName": {
                    "Fn::If": [
                        "ShouldNotCreateEnvResources",
                        "<%= props.roleName %>",
                        {

                            "Fn::Join": [
                                "",
                                [
                                    "<%= props.roleName %>",
                                    "-",
                                    {
                                        "Ref": "env"
                                    }
                                ]
                            ]
                        }
                    ]
                },
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "lambda.amazonaws.com"
                                ]
                            },
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        }
                    ]
                },
                "Policies": [
                    {
                        "PolicyName": "<%= props.lexPolicyName %>",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "lex:*"
                                    ],
                                    "Resource": "*"
                                }
                            ]
                        }
                    },
                    {
                        "PolicyName": "IAMPolicy",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "iam:*"
                                    ],
                                    "Resource": "*"
                                }
                            ]
                        }
                    },
                    {
                        "PolicyName": "<%= props.lambdaPolicyName %>",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "lambda:*"
                                    ],
                                    "Resource": "*"
                                }
                            ]
                        }
                    }
                ]
            }
        },
        "LambdaFunctionOutputs<%= props.shortId %>": {
            "Type": "Custom::LambdaCallout",
            "Properties": {
                "ServiceToken": {
                    "Fn::GetAtt": [
                        "LambdaFunction",
                        "Arn"
                    ]
                },
                "region": {
                    "Ref": "AWS::Region"
                  },
                  "lexRegion": {
                    "Fn::FindInMap": ["RegionMapping", { "Ref": "AWS::Region" }, "lexRegion"]
                  }
            }
        },
        "CognitoUnauthPolicy": {
            "Type": "AWS::IAM::Policy",
            "Properties": {
              "PolicyName": "<%= props.unauthPolicyName %>",
              "Roles": [
                {"Ref": "unauthRoleName" }
              ],
              "PolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Action": [
                            "lex:PostContent",
                            "lex:PostText"
                        ],
                        "Effect": "Allow",
                        "Resource": [
                            {

                                "Fn::Join": [
                                    "",
                                    [
                                      "arn:aws:lex:",
                                      {
                                        "Ref": "AWS::Region"
                                      },
                                      ":",
                                      { "Fn::Select" : [ "4", { "Fn::Split": [":", {"Ref": "authRoleArn"}]}]},
                                      ":bot:",
                                      {
                                        "Fn::If": [
                                            "ShouldNotCreateEnvResources",
                                            "<%= props.botName %>",
                                            {

                                                "Fn::Join": [
                                                    "",
                                                    [
                                                        "<%= props.botName %>",
                                                        "_",
                                                        {
                                                            "Ref": "env"
                                                        }
                                                    ]
                                                ]
                                            }
                                        ]
                                      },
                                      ":*"
                                    ]
                                ]
                            }
                        ]
                    }
                ]
              }
            }
        },
        "CognitoAuthPolicy": {
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": "<%= props.authPolicyName %>",
              "Roles": [
                {"Ref": "authRoleName" }
              ],
              "PolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Action": [
                            "lex:PostContent",
                            "lex:PostText"
                        ],
                        "Effect": "Allow",
                        "Resource": [
                            {

                                "Fn::Join": [
                                    "",
                                    [
                                      "arn:aws:lex:",
                                      {
                                        "Ref": "AWS::Region"
                                      },
                                      ":",
                                      { "Fn::Select" : [ "4", { "Fn::Split": [":", {"Ref": "authRoleArn"}]}]},
                                      ":bot:",
                                      {
                                        "Fn::If": [
                                            "ShouldNotCreateEnvResources",
                                            "<%= props.botName %>",
                                            {

                                                "Fn::Join": [
                                                    "",
                                                    [
                                                        "<%= props.botName %>",
                                                        "_",
                                                        {
                                                            "Ref": "env"
                                                        }
                                                    ]
                                                ]
                                            }
                                        ]
                                      },
                                      ":*"
                                    ]
                                ]
                            }
                        ]
                    }
                ]
              }
            }
        }
    },
    "Outputs": {
        "Region": {
            "Value": {
                "Fn::FindInMap": ["RegionMapping", { "Ref": "AWS::Region" }, "lexRegion"]
            }
        },
        "BotName":  {
            "Value": {
                "Fn::If": [
                    "ShouldNotCreateEnvResources",
                    "<%= props.botName %>",
                    {

                        "Fn::Join": [
                            "",
                            [
                                "<%= props.botName %>",
                                "_",
                                {
                                    "Ref": "env"
                                }
                            ]
                        ]
                    }
                ]
            }
        },
        "FunctionArn": {
            "Value": {
                "Fn::GetAtt": [
                    "LambdaFunction",
                    "Arn"
                ]
            }
        }
    }
}
